--[[
	FONTE: 
		* https://github.com/BlockMen/hud
		* https://github.com/BlockMen/hud/blob/master/init.lua
]]--

lunogauges.huds.stamin_player = {}
lunogauges.huds.gaugestamin = {}
lunogauges.huds.stamin_timer = 0 --Contador geral
lunogauges.huds.stamin_delay = 180 --3*60 --Acada 3 minutos 1 ponto de stamina e decrecidode todos os players.
lunogauges.huds.stamin_timerdamage = 0 --Contador de Intervalo de dano
lunogauges.huds.stamin_delaydamage = 3 --Acada 3 segundo 1 ponto de HP é decrecido do player faminto (100% do HP em 1 minuto).
lunogauges.huds.stamin_timerrescue = 0 --Contador de Intervalo de cura
lunogauges.huds.stamin_delayrescue = 9 --Acada 9 segundo 1 ponto de HP é decrecido do player faminto (100% do HP em 3 minuto).

lunogauges.huds.get_stamin  = function(playername)
	local stamin = lunomods.getCharValue(playername, "stamin")
	if stamin == nil or type(stamin)~="number" or stamin>20 then
		stamin = 20
	elseif stamin<0 then
		stamin = 0
	end
	lunomods.setCharValue(playername, "stamin", stamin)
	return stamin
end

lunogauges.huds.set_stamin = function(playername, value)
	if value == nil or type(value)~="number" or value>20 then
		value = 20
	elseif value<0 then
		value = 0
	end
	lunomods.setCharValue(playername, "stamin", value)
end

lunogauges.huds.add_stamin  = function(player, add)
	local newStamin = lunogauges.huds.get_stamin(playername)
	if (add < 0 and newStamin>0) or (add > 0 and newStamin<20)  then 
		newStamin=newStamin + add
		lunogauges.huds.set_stamin(playername, newStamin)
	end
end

lunogauges.huds.check_angry  = function(player, dtime)
	if player~= nil and player:is_player() then
		local playername = player:get_player_name()

		lunogauges.huds.stamin_timer = lunogauges.huds.stamin_timer + dtime
		if lunogauges.huds.stamin_timer >= lunogauges.huds.stamin_delay then
			lunogauges.huds.stamin_timer = 0
			lunogauges.huds.add_stamin(playername, -1)
		end
		
		lunogauges.huds.stamin_timerdamage = lunogauges.huds.stamin_timerdamage + dtime
		if lunogauges.huds.stamin_timerdamage >= lunogauges.huds.stamin_delaydamage then
			if lunogauges.huds.get_stamin(playername) == 0 then
				lunogauges.huds.stamin_timerdamage = 0
				if player:get_hp() > 0 then
					player:set_hp(player:get_hp() - 1)
					minetest.sound_play("sfx_starvation", {
						pos=player:getpos(),
						gain = 6.0,
						max_hear_distance = 5,
					})
				end
			end
		end
		
		lunogauges.huds.stamin_timerrescue = lunogauges.huds.stamin_timerrescue + dtime
		if lunogauges.huds.stamin_timerrescue >= lunogauges.huds.stamin_delayrescue then
			if lunogauges.huds.get_stamin(playername) > 0 then
				lunogauges.huds.stamin_timerrescue = 0
				if player:get_hp() > 0 and player:get_hp() < 20 then
					player:set_hp(player:get_hp() + 1)
					lunogauges.huds.add_stamin(playername, -1) --Se curar acelera a fome!!!
				end
			end
		end

		lunogauges.huds.update_stamin(player)
	end
end

lunogauges.huds.add_stamin = function(player)
	if player~= nil and player:is_player() then
		local playername = player:get_player_name()
		if lunogauges.huds.gaugestamin[playername] == nil then
			lunogauges.huds.gaugestamin[playername] = {}
		end
		
		lunogauges.huds.gaugestamin[playername].bg = player:hud_add({
			hud_elem_type = "statbar",
			position = {x=0.5,y=1},
			size = {x=32, y=32}, --{x=24, y=24},
			text = "hud_stamin_bg.png", --"default_apple.png^[brighten",
			number = 20,
			alignment = {x=-1,y=-1},
			offset = {x=15, y=-97}, --{x=-215, y=-97}, 
		})

		lunogauges.huds.gaugestamin[playername].fg = player:hud_add({
			hud_elem_type = "statbar",
			position = {x=0.5,y=1},
			size = {x=32, y=32}, --{x=24, y=24},
			text = "hud_stamin_fg.png", --"default_apple.png", 
			number = lunogauges.huds.get_stamin(playername),
			alignment = {x=-1,y=-1},
			offset = {x=15, y=-97}, --{x=-215, y=-97},
		})
		lunogauges.huds.stamin_player[playername] = lunogauges.huds.get_stamin(playername)
	end
end

lunogauges.huds.update_stamin = function(player)
	if player~= nil and player:is_player() then
		local playername = player:get_player_name()
		local stamin = lunogauges.huds.get_stamin(playername)
		if lunogauges.huds.stamin_player[playername]== nil or lunogauges.huds.stamin_player[playername] ~= stamin   then
			lunogauges.huds.stamin_player[playername] = stamin
			if lunogauges.huds.gaugestamin[playername]~=nil and lunogauges.huds.gaugestamin[playername].fg ~= nil then
				player:hud_change(lunogauges.huds.gaugestamin[playername].bg, "number", 20) --Por causa de bug do 0.4.10 tem q atualizar de vez em quando o background tb.
				player:hud_change(lunogauges.huds.gaugestamin[playername].fg, "number", stamin)
			else
				lunogauges.huds.add_stamin(player)
			end
		end
	end
end


